/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bhims;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Rein
 */
public class ScrapeBySearch extends javax.swing.JInternalFrame {

    /**
     * Creates new form ScrapeBySearch
     */
    public ScrapeBySearch() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        AccountPathjTextField1 = new javax.swing.JTextField();
        BrowseAccountjButton1 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        KeywordsjTextArea1 = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        RatiojSpinner1 = new javax.swing.JSpinner();
        jLabel4 = new javax.swing.JLabel();
        ThreadsjSpinner2 = new javax.swing.JSpinner();
        jLabel5 = new javax.swing.JLabel();
        StartStopjButton2 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        ResultsjTable1 = new javax.swing.JTable();
        jLabel7 = new javax.swing.JLabel();
        ActionsjComboBox1 = new javax.swing.JComboBox();
        DoActionjButton3 = new javax.swing.JButton();
        RowCountjLabel8 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane6 = new javax.swing.JScrollPane();
        ConsolejTextArea2 = new javax.swing.JTextArea();

        setBackground(java.awt.SystemColor.window);
        setClosable(true);
        setTitle("Scrape Users By Search");

        jTabbedPane1.setBackground(new java.awt.Color(255, 255, 255));
        jTabbedPane1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setText("<html> 1. Load Accounts (Text File, one account per line in any of the following format:) <br> Username:Password <br> Username:Password:IP:Port or <br>\nUsername:Password:IP:Port:ProxyUsername:Proxy Password </html> ");

        AccountPathjTextField1.setEditable(false);
        AccountPathjTextField1.setText("Text File Location");

        BrowseAccountjButton1.setText("Browse..");
        BrowseAccountjButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BrowseAccountjButton1ActionPerformed(evt);
            }
        });

        jLabel2.setText("2. Type your Keywords, 1 per line");

        KeywordsjTextArea1.setColumns(20);
        KeywordsjTextArea1.setFont(new java.awt.Font("Monospaced", 0, 12)); // NOI18N
        KeywordsjTextArea1.setRows(5);
        KeywordsjTextArea1.setText("Lose Weight\nWeight loss\nDiet\nDiet Programs");
        jScrollPane2.setViewportView(KeywordsjTextArea1);

        jLabel3.setText("<html>3. Select the ratio of users to scrape for every keyword. <br>\nExample: Setting it to 20 means the bot will find 20 users for each keyword.\n</html>");

        RatiojSpinner1.setValue(100);

        jLabel4.setText("<html>\n4. Set the number of parallel threads. <br>\nThe higher the thread count, the faster the bot will finish the job.\n</html>");

        ThreadsjSpinner2.setValue(1);

        jLabel5.setText("<html>\n5. Press this button to start/stop scraping Users:\n</html>");

        StartStopjButton2.setText("Start Scraping");
        StartStopjButton2.setEnabled(false);
        StartStopjButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                StartStopjButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane2)
                    .addComponent(jLabel2)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(AccountPathjTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 443, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(BrowseAccountjButton1))
                    .addComponent(jLabel1)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(RatiojSpinner1, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE)
                            .addComponent(ThreadsjSpinner2)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(StartStopjButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(16, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(AccountPathjTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(BrowseAccountjButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(RatiojSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ThreadsjSpinner2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(StartStopjButton2))
                .addContainerGap(46, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Configuration", null, jPanel1, "");

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));

        jLabel6.setText("Scraped Users will be shown here:");

        ResultsjTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "User", "Keyword"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        ResultsjTable1.addContainerListener(new java.awt.event.ContainerAdapter() {
            public void componentAdded(java.awt.event.ContainerEvent evt) {
                ResultsjTable1ComponentAdded(evt);
            }
            public void componentRemoved(java.awt.event.ContainerEvent evt) {
                ResultsjTable1ComponentRemoved(evt);
            }
        });
        ResultsjTable1.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                ResultsjTable1PropertyChange(evt);
            }
        });
        jScrollPane4.setViewportView(ResultsjTable1);

        jLabel7.setText("Select an action:");

        ActionsjComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Clear Results Table", "Delete Selected", "Remove Duplicate Users", "Copy Users to Clipboard", "Save to Text File" }));

        DoActionjButton3.setText("Do Action");
        DoActionjButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DoActionjButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(ActionsjComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(DoActionjButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addComponent(jLabel6)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(RowCountjLabel8))
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 526, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(18, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(RowCountjLabel8))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 290, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(ActionsjComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(DoActionjButton3))
                .addContainerGap(18, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Results", jPanel2);

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));

        ConsolejTextArea2.setEditable(false);
        ConsolejTextArea2.setColumns(20);
        ConsolejTextArea2.setFont(new java.awt.Font("Monospaced", 0, 12)); // NOI18N
        ConsolejTextArea2.setRows(5);
        ConsolejTextArea2.setText("Console ready..\n");
        jScrollPane6.setViewportView(ConsolejTextArea2);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane6, javax.swing.GroupLayout.DEFAULT_SIZE, 554, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane6, javax.swing.GroupLayout.DEFAULT_SIZE, 392, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("Console", jPanel3);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BrowseAccountjButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BrowseAccountjButton1ActionPerformed
        JFileChooser fc = new JFileChooser();
        fc.setFileFilter(new FileNameExtensionFilter("Text Files", "txt"));


        int returnVal = fc.showOpenDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            AccountPathjTextField1.setText(fc.getSelectedFile().getAbsolutePath());
            CheckIfOkToScrape();
        }


    }//GEN-LAST:event_BrowseAccountjButton1ActionPerformed

    private void StartStopjButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_StartStopjButton2ActionPerformed
        if ("Start Scraping".equals(StartStopjButton2.getText())) {
            StartStopjButton2.setText("Stop Scraping");
            StartScraping();
            jTabbedPane1.setSelectedIndex(2);
        } else {
            StopScraping();
        }
    }//GEN-LAST:event_StartStopjButton2ActionPerformed

    private void DoActionjButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DoActionjButton3ActionPerformed
        // TODO add your handling code here:
        switch (ActionsjComboBox1.getSelectedItem().toString()) {
            case "Clear Results Table":
                ClearResultsTable();
                RowCountjLabel8.setText(String.valueOf(ResultsjTable1.getRowCount()));
                break;

            case "Delete Selected":
                DeleteSelectedRows();
                RowCountjLabel8.setText(String.valueOf(ResultsjTable1.getRowCount()));
                break;

            case "Remove Duplicate Users":                
                RemoveDuplicateRows();
                RowCountjLabel8.setText(String.valueOf(ResultsjTable1.getRowCount()));
                break;

            case "Copy Users to Clipboard":
                CopyUsersToClipBoard();
                break;

            case "Save to Text File":
                SaveUsersToTextFile();
                break;
        }
    }//GEN-LAST:event_DoActionjButton3ActionPerformed

    private void ResultsjTable1PropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_ResultsjTable1PropertyChange
        
    }//GEN-LAST:event_ResultsjTable1PropertyChange

    private void ResultsjTable1ComponentAdded(java.awt.event.ContainerEvent evt) {//GEN-FIRST:event_ResultsjTable1ComponentAdded
        
    }//GEN-LAST:event_ResultsjTable1ComponentAdded

    private void ResultsjTable1ComponentRemoved(java.awt.event.ContainerEvent evt) {//GEN-FIRST:event_ResultsjTable1ComponentRemoved
             
    }//GEN-LAST:event_ResultsjTable1ComponentRemoved

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField AccountPathjTextField1;
    private javax.swing.JComboBox ActionsjComboBox1;
    private javax.swing.JButton BrowseAccountjButton1;
    public javax.swing.JTextArea ConsolejTextArea2;
    private javax.swing.JButton DoActionjButton3;
    public javax.swing.JTextArea KeywordsjTextArea1;
    public javax.swing.JSpinner RatiojSpinner1;
    public javax.swing.JTable ResultsjTable1;
    private javax.swing.JLabel RowCountjLabel8;
    private javax.swing.JButton StartStopjButton2;
    private javax.swing.JSpinner ThreadsjSpinner2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    public javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JTabbedPane jTabbedPane1;
    // End of variables declaration//GEN-END:variables
    public ArrayBlockingQueue AccountsBlockingQueue = new ArrayBlockingQueue(5000);
    public ArrayBlockingQueue KeywordsBlockingQueue = new ArrayBlockingQueue(5000);
    ArrayList RunningThreadsList = new ArrayList();
    Integer finishedThreads;

    private void StartScraping() {
        finishedThreads = 0;
        RunningThreadsList.clear();
        CommonFunctions cf = new CommonFunctions();

        AccountsBlockingQueue.clear();
        cf.LoadTextToBlockingQueue(AccountPathjTextField1.getText(), AccountsBlockingQueue);

        KeywordsBlockingQueue.clear();
        cf.loadStringArraytoBlockingQueue(KeywordsjTextArea1.getText().split("\\n"), KeywordsBlockingQueue);

        for (int i = 0; i < (Integer) ThreadsjSpinner2.getValue(); i++) {
            ScrapeUsersBySearchThread subsThread = new ScrapeUsersBySearchThread();
            subsThread.ParentWindow = this;
            Thread t = new Thread(subsThread);
            t.setName(String.valueOf(i + 1));
            RunningThreadsList.add(t);
            t.start();
        }
        // Create Threads
        // Set Thread parent Value
    }

    boolean AllThreadsFinished() {
        finishedThreads += 1;
        if (finishedThreads == RunningThreadsList.size()) {
            StartStopjButton2.setText("Start Scraping");
            return true;
        }
        return false;
    }

    private void StopScraping() {

        for (int i = 0; i < RunningThreadsList.size(); i++) {
            Thread t = (Thread) RunningThreadsList.get(i);
            t.interrupt();
        }
        StartStopjButton2.setText("Start Scraping");
        ConsolejTextArea2.append("Threads Interrupted by User." + "\n");
    }

    private void ClearResultsTable() {
        DefaultTableModel tbm = (DefaultTableModel) ResultsjTable1.getModel();
        tbm.setRowCount(0);
    }

    private void DeleteSelectedRows() {
        DefaultTableModel tbm = (DefaultTableModel) ResultsjTable1.getModel();
        int[] SelectedRows = ResultsjTable1.getSelectedRows();

        for (int i = SelectedRows.length - 1; i >= 0; i--) {
            int SelectedRowI = SelectedRows[i];

            tbm.removeRow(SelectedRowI);
        }
    }

    private void RemoveDuplicateRows() {
        DefaultTableModel tbm = (DefaultTableModel) ResultsjTable1.getModel();

        int i = ResultsjTable1.getRowCount() - 1;
        do {
            if (i > ResultsjTable1.getRowCount() - 1) {
                i = ResultsjTable1.getRowCount() - 1;
            }
            String User = (String) tbm.getValueAt(i, 0);
            for (int j = i - 1; j >= 0; j--) {
                String UserToMatch = (String) tbm.getValueAt(j, 0);
                if (User == null ? UserToMatch == null : User.equals(UserToMatch)) {
                    tbm.removeRow(j);
                }
            }
            i--;
            if (i < 0) {
                break;
            }
        } while (i >= 0);

    }

    private void CopyUsersToClipBoard() {
        DefaultTableModel tbm = (DefaultTableModel) ResultsjTable1.getModel();
        String UserString = "";
        for (int i = 0; i < ResultsjTable1.getRowCount() - 1; i++) {
            UserString = UserString + (String) tbm.getValueAt(i, 0) + "\n";
        }
        StringSelection selection = new StringSelection(UserString.trim());
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(selection, selection);
        JOptionPane.showMessageDialog(this, "Copied users to clipboard!");
    }

    private void SaveUsersToTextFile() {
        DefaultTableModel tbm = (DefaultTableModel) ResultsjTable1.getModel();
        String UserString = "";
        
        for (int i = 0; i < ResultsjTable1.getRowCount() - 1; i++) {
            UserString = UserString + (String) tbm.getValueAt(i, 0) + "\r\n";
        }
        
        UserString = UserString.trim();
        
        JFileChooser fc = new JFileChooser();
        fc.setFileFilter(new FileNameExtensionFilter("Text Files", "txt"));


        int returnVal = fc.showSaveDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            BufferedWriter writer = null;
            try {
                String Filename = fc.getSelectedFile().getAbsolutePath();
                if (Filename.contains(".txt") == false) {
                    Filename = Filename + ".txt";
                }
                writer = new BufferedWriter( new FileWriter(Filename));
                writer.write(UserString);
               
                writer.close();
            } catch (IOException ex) {
                
            } finally {
                try {
                    writer.close();
                } catch (IOException ex) {
                
                }
            }
        }
    }

    private void CheckIfOkToScrape() {
        if (!"Text File Location".equals(AccountPathjTextField1.getText()) && !"".equals(KeywordsjTextArea1.getText())) {
            StartStopjButton2.setEnabled(true);
        }
    }
}
